//
// CCS C Compiler
// PCM 4.056
//

#include<16f88.h>
 
#fuses INTRC,nowdt,noput
#define XTAL_CLOCK 8000000

#use delay(clock=XTAL_CLOCK)

#use fast_io(B)

volatile int8  tmrtick,tmrbatut;




// tick de aprox 5ms (200 ticks por segundo)
#define TIMER0_BASE 0xD9



const int8 batut[] =
{
	25,
	50,
	75,
	100,
	100,
	100,
	100,

	0,
	0,
	0,
	0,
	0,

	25,
	50,
	100,
	200,
	255,
	255,
	255,
	255,
	255,
	255,
	255,
	255,
	255,

	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
};	


#define NUM_FLASH sizeof(batut)

int8 index=-1;


void tsk_flasher_batut(void)
{
	index++; if (index > sizeof(batut)-1) index = 0;

	set_pwm1_duty(255-batut[index]);
	delay_ms(25);
}	

#INT_TIMER0
void isr_timer0()
{
	// Recarga el timer 0 (a lo bestia)
	set_timer0( TIMER0_BASE );

	if (tmrtick) tmrtick--;

	if (tmrbatut) tmrbatut--;
}	


void init_timer0( void )
{
	setup_timer_0( RTCC_INTERNAL | RTCC_DIV_256 ); // Tclock = 1 us
	set_timer0( TIMER0_BASE );
	enable_interrupts( INT_TIMER0 );
}



void main(void)
{
	init_timer0();
	tmrtick = 0;

	set_tris_b(0xfe);
	
	output_high(PIN_B0);

	port_b_pullups(TRUE);
	
	enable_interrupts(GLOBAL);

	setup_timer_2(T2_DIV_BY_1, 254, 1);
	setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM


	for(;;)
	{
		tsk_flasher_batut();
	}
}

